<?php

use app\portal\model\PortalCategoryModel;

/**
 * Created by PhpStorm.
 * User: xionghao
 * Date: 2017/10/15
 * Time: 22:36
 */
function sub_title($t, $len = 50)
{
    $text = $t;
    return mb_strlen($text) <= $len ? $text : (mb_substr($text, 0, $len, 'utf-8') . "...");
}

function isNew($article)
{
    if (time() - $article['published_time'] > 3600 * 24 * 7) {
        return false;
    } else {
        return true;
    }
}

function getDownloads($limit = 5)
{
    $downloads = \app\portal\model\DownloadModel::limit($limit)->order('create_time', 'desc')->select();
    return $downloads;
}


function pr($var)
{
    if (config('app_debug')) {
        $template = PHP_SAPI !== 'cli' ? '<pre>%s</pre>' : "\n%s\n";
        printf($template, print_r($var, true));
    }
}

function get_category($cid)
{
    $portalCategory = new PortalCategoryModel();
    $category = $portalCategory->where('id', $cid)->where('delete_time', 0)->find();
    return $category;
}

function subCategory($cid)
{
    $portalCategory = new PortalCategoryModel();
    $categorys = $portalCategory->where('parent_id', $cid)->where('delete_time', 0)->select();
//    var_dump($categorys);
//    exit;
    return $categorys;
}

function allSubCategory($cid)
{
    $portalCategory = new PortalCategoryModel();
    $categorys = $portalCategory->where('parent_id', $cid)->where('delete_time', 0)->select();
    var_dump($categorys);

    if ($categorys) {
        $results = [];
        foreach ($categorys->toArray() as $category) {
            $childs = allSubCategory($category['id']);
            $results = array_merge($childs, $results);
        }
        $results = array_merge($results, $categorys->toArray());
        return $results;
    } else {
        return $categorys;
    }
}

function allSubCategoryIds($cid)
{
    $portalCategory = new PortalCategoryModel();
    $categorys = $portalCategory->where('parent_id', $cid)->where('delete_time', 0)->select();
    $results = [];
    if ($categorys) {
        foreach ($categorys->toArray() as $category) {
            $results[] = $category['id'];
            $childs = allSubCategoryIds($category['id']);
            if ($childs) {
                foreach ($childs as $child) {
                    $results[] = $child;
                }
            }
        }
        return $results;
    } else {
        return [];
    }
}

function status($value)
{
    if ($value) {
        return '<span class="layui-badge layui-bg-green">开启</span>';
    } else {
        return '<span class="layui-badge">关闭</span>';
    }
}

function user_type($status)
{
    switch ($status) {

        case 1:
            return '管理员';
            break;
        case 2:
            return "个人";
            break;
        case 3:
            return "单位";
            break;
    }
}


function getNa($id)
{

}

function volunteerStatus($value)
{
    switch ($value) {
        case 0:
            return '公共突发事件应急救援';
            break;
        case  1:
            return '大型活动现场救援';
            break;
        case  2:
            return '义诊';
            break;
        case  3:
            return '献血';
            break;
        case  4:
            return '业务查房';
            break;
        case  5:
            return '导医';
            break;
        case  6:
            return '讲学';
            break;
        case  7:
            return '对口帮扶';
            break;
        case  8:
            return '健康咨询';
            break;
        case  9:
            return '心理辅导';
            break;
        case  10:
            return '其他';
            break;
    }
}

function getNavMenu($nav_id)
{

}